package cn.exrick.xboot.config.obs;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ObsConfig {

    @Value("${ctyun.obs.access-key}")
    private String accessKey;

    @Value("${ctyun.obs.secret-key}")
    private String secretKey;

    @Value("${ctyun.obs.bucket-name}")
    private String bucketName;

    @Value("${ctyun.obs.endpoint}")
    private String endpoint;

    @Bean
    public AmazonS3 amazonS3() {
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        ClientConfiguration awsClientConfig = new ClientConfiguration();
        awsClientConfig.setSignerOverride("AWSS3V4SignerType");
        awsClientConfig.setProtocol(Protocol.HTTP);
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new
                        AWSStaticCredentialsProvider(credentials))
                .withClientConfiguration(awsClientConfig)
                .withEndpointConfiguration(new
                        AwsClientBuilder.EndpointConfiguration(endpoint, ""))
                .disableChunkedEncoding()
                .enablePathStyleAccess()
                .build();
        return s3Client;
    }
}
